package com.Tread;


import com.API.MyFirstAnnotations;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TakeDelivery {


    private static LinkedBlockingDeque<String> queue =  new LinkedBlockingDeque<>();

    private static LinkedBlockingDeque<String> receiveQueue =  new LinkedBlockingDeque<>();

    private static final String END_FLAG = "null";

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        Path path = Paths.get("src/com/annotations");
        System.out.println(path.getFileName());
        String[] split = "src/com/annotations".split("rc/");
        System.out.println(split[1]);
        putClassNameToQueue(path);
        getClassName();

//        Path globalPath = Paths.get("com/annotations");
//        try(Stream<Path> children = Files.list(globalPath)) {
//            for (Path e : children.collect(Collectors.toList())) {
//                if (e.getFileName().equals("annotations")) {
//
//                    break;
//                }
//            }
//        }


    }


    /**
     * 将目录下的文件类名添加到反射队列
     * @param path 文件名
     * @throws  IOException IO异常
     * @throws InterruptedException 打断异常
     */
    public static void putClassNameToQueue(Path path) throws IOException, InterruptedException {
        try(Stream<Path> children = Files.list(path)) {
            for (Path e : children.collect(Collectors.toList())) {
                System.out.println(e.getParent()+">>>>>>>>>>>>");
                String FileName = e.getParent() +"\\"+e.getFileName();

                String ClassName = FileName.replaceAll("\\\\",".");
                String[] split = ClassName.split("rc.");
//                String[] split1 = split[1].split(".java");
                String className = split[1].replace(".java","");

                System.out.println(className);
                queue.put(className);
            }
        }
    }

    /**
     * 从类名队列获取类名
     */
    public static void getClassName() throws InterruptedException, ClassNotFoundException {
        boolean end = false;
        while( !end ) {
            String className = queue.take();
            if (className.equals(END_FLAG)) {
                queue.put(END_FLAG);
                end = true;
            }
            else {
                getAnnotationsValue(MyFirstAnnotations.class,className);
            }
        }

    }


    public static void getAnnotationsValue(Class<? extends Annotation> ann,String className) throws ClassNotFoundException, InterruptedException {

        Class<?> aClass = Class.forName(className);
        MyFirstAnnotations annotation = aClass.getAnnotation(MyFirstAnnotations.class);
        assert annotation != null : "注解无法取到";
        receiveQueue.put(annotation.description());
        System.out.println(annotation.description());

    }





}
